//
//  ByShopCarPayVC.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/30.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByShopCarPayVC.h"

@interface ByShopCarPayVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;
@end

@implementation ByShopCarPayVC

#pragma mark - view method
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"支付";
    
    [self addLeftBackBarButtonItem];
    
    [self createTableView];
}

#pragma mark - 创建视图
- (void)createTableView {
    
    self.table = [[UITableView alloc] init];
    self.table.backgroundColor = BY_BG_COLOR;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0)).priorityMedium();
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-0);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        }
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return 2;
    } else {
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 消费金额
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"originMoneyCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"originMoneyCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            // view
            UILabel *label = [[UILabel alloc] init];
            label.adjustsFontSizeToFitWidth = true;
            label.font = FONT(15);
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor blackColor];
            [cell.contentView addSubview:label];
            label.tag = 10;
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(cell.contentView).insets(UIEdgeInsetsMake(0, 10, 0, 10));
            }];
        }
        UILabel *label = [cell.contentView viewWithTag:10];
        label.text = [NSString stringWithFormat:@"消费金额：￥%.2lf", _money];
        
        return cell;
    }
    // 积分抵扣和优惠券
    else if (indexPath.section == 1) {
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"deductionCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"deductionCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            // view
            UILabel *label = [[UILabel alloc] init];
            label.adjustsFontSizeToFitWidth = true;
            label.font = FONT(15);
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor blackColor];
            [cell.contentView addSubview:label];
            label.tag = 10;
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(cell.contentView).insets(UIEdgeInsetsMake(0, 10, 0, 10));
            }];
            UIView *line = [UIView new];
            line.backgroundColor = BY_LINE_COLOR;
            [cell.contentView addSubview:line];
            line.tag = 11;
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.equalTo(cell);
                make.height.equalTo(@1);
            }];
        }
        UILabel *label = [cell.contentView viewWithTag:10];
        UIView *line = [cell.contentView viewWithTag:11];

        if (indexPath.row == 0) {
            NSString *deductionMoney = [NSString stringWithFormat:@"%.2lf", _deduction];
            NSString *deductionScore = [NSString stringWithFormat:@"%.0lf", _deduction * 100];
            NSString *deductionText = [NSString stringWithFormat:@"使用蜜积分%@抵扣%@元", deductionScore, deductionMoney];
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:deductionText];
            [attr addAttribute:NSForegroundColorAttributeName value:BY_GOLD_COLOR range:[deductionText rangeOfString:deductionScore]];
            [attr addAttribute:NSForegroundColorAttributeName value:BY_GOLD_COLOR range:[deductionText rangeOfString:deductionMoney]];
            label.attributedText = attr.copy;
            
            if (_coupons != nil) {
                line.hidden = false;
            } else {
                line.hidden = true;
            }
        }
        else {
            NSString *couponsMoney = [NSString stringWithFormat:@"%.2lf", (CGFloat)_coupons.money];
            NSString *couponsText = [NSString stringWithFormat:@"使用优惠券抵扣%@元", couponsMoney];
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:couponsText];
            [attr addAttribute:NSForegroundColorAttributeName value:BY_GOLD_COLOR range:[couponsText rangeOfString:couponsMoney]];
            label.attributedText = attr.copy;
            
            line.hidden = true;
        }
        
        return cell;
    }
    
    // 实际付款金额
    else if (indexPath.section == 2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"realMoneyCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"realMoneyCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            // view
            UILabel *titleLabel = [[UILabel alloc] init];
            titleLabel.font = FONT(16);
            titleLabel.text = @"实际付款金额";
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel.textColor = [UIColor blackColor];
            [cell.contentView addSubview:titleLabel];
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(@10);
                make.left.right.equalTo(@0);
                make.height.equalTo(@20);
            }];
            UILabel *contentLabel = [[UILabel alloc] init];
            contentLabel.font = FONT(44);
            contentLabel.textAlignment = NSTextAlignmentCenter;
            contentLabel.textColor = BY_RED_COLOR;
            [cell.contentView addSubview:contentLabel];
            contentLabel.tag = 10;
            [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.equalTo(@0);
                make.top.equalTo(titleLabel.mas_bottom);
            }];
            
        }
        UILabel *contentLabel = [cell.contentView viewWithTag:10];
        
        NSString *text = [NSString stringWithFormat:@"￥%.2lf", _money - _deduction - _coupons.money];
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];
        [attrStr addAttribute:NSFontAttributeName value:FONT(30) range:[text rangeOfString:@"￥"]];
        [attrStr addAttribute:NSFontAttributeName value:FONT(30) range:NSMakeRange(text.length - 2, 2)];

        contentLabel.attributedText = attrStr.copy;
        
        return cell;
    }
    // 付款方式
    else if (indexPath.section == 3) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"payTypeCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"payTypeCell"];
            cell.backgroundColor = [UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            // view
            UILabel *titleLabel = [[UILabel alloc] init];
            titleLabel.backgroundColor = tableView.backgroundColor;
            titleLabel.font = FONT(14);
            titleLabel.text = @"付款方式";
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel.textColor = [UIColor lightGrayColor];
            [cell.contentView addSubview:titleLabel];
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(@80);
                make.height.equalTo(@20);
                make.top.centerX.equalTo(@0);
            }];
            // 线条
            UIView *line = [UIView new];
            line.backgroundColor = BY_LINE_COLOR;
            [cell.contentView insertSubview:line atIndex:0];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(cell);
                make.height.equalTo(@1);
                make.centerY.equalTo(titleLabel);
            }];
            // 微信
            UIImageView *wechatIconView = [UIImageView new];
            wechatIconView.userInteractionEnabled = true;
            wechatIconView.image = [UIImage imageNamed:@"微信"];
            [cell.contentView addSubview:wechatIconView];
            [wechatIconView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(80, 80));
                make.centerX.equalTo(cell).multipliedBy(0.6);
                make.top.equalTo(titleLabel.mas_bottom).offset(20);
            }];
            UILabel *wechatLabel = [[UILabel alloc] init];
            wechatLabel.text = @"微信支付";
            wechatLabel.font = FONT(15);
            wechatLabel.textAlignment = NSTextAlignmentCenter;
            wechatLabel.textColor = [UIColor lightGrayColor];
            [cell.contentView addSubview:wechatLabel];
            [wechatLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(wechatIconView.mas_bottom).offset(5);
                make.height.equalTo(@20);
                make.width.equalTo(@100);
                make.centerX.equalTo(wechatIconView);
            }];
            
            // 支付宝
            UIImageView *alipayIconView = [UIImageView new];
            alipayIconView.userInteractionEnabled = true;
            alipayIconView.image = [UIImage imageNamed:@"支付宝"];
            [cell.contentView addSubview:alipayIconView];
            [alipayIconView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.top.equalTo(wechatIconView);
                make.centerX.equalTo(cell).multipliedBy(1.4);
            }];
            UILabel *alipayLabel = [[UILabel alloc] init];
            alipayLabel.text = @"支付宝支付";
            alipayLabel.font = FONT(15);
            alipayLabel.textAlignment = NSTextAlignmentCenter;
            alipayLabel.textColor = [UIColor lightGrayColor];
            [cell.contentView addSubview:alipayLabel];
            [alipayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.top.equalTo(wechatLabel);
                make.centerX.equalTo(alipayIconView);
            }];
            
        }

        
        return cell;
    }
    
    

    
    
    return UITableViewCell.new;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return 50;
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            return _deduction == 0 ? 0 : 50;
        } else {
            return _coupons == nil ? 0 : 50;
        }
    } else if (indexPath.section == 2) {
        return 100;
    } else if (indexPath.section == 3) {
        return 200;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section != 0) {
        return 10;
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return UIView.new;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}



@end
